import io
from PIL import Image, ImageDraw
from PyPDF2 import PdfFileWriter, PdfFileReader

memo = {}



def getPdfReader(filename):
    reader = memo.get(filename, None)
    if reader is None:
        reader = PdfFileReader(filename, strict=False)
        memo[filename] = reader
    return reader


def _run_convert(filename, page, res=120):
    idx = page + 1
    pdfile = getPdfReader(filename)
    pageObj = pdfile.getPage(page)
    dst_pdf = PdfFileWriter()
    dst_pdf.addPage(pageObj)
    pdf_bytes = io.BytesIO()
    dst_pdf.write(pdf_bytes)
    pdf_bytes.seek(0)
    # img = Image(file=pdf_bytes, resolution=res)
    # img.format = 'png'
    # img.compression_quality = 90
    # # img.background_color = Color("white")
    # img_path = '%s%d.png'% (filename[:filename.rindex('.')], idx)
    # img.save(filename=img_path)
    # img.destroy()

_run_convert("demo.pdf", 1)